package com.smartalarmclock.Model;

import java.util.HashSet;
import java.util.Set;

import android.content.Context;

abstract class Contact extends ReferencedDataEntity implements IContact {
	
	// Database Constants
	
	public static final String ContactTableName = "Contact";
	
	protected static final String c_ContactColumnNameType = "Type";
	protected static final String c_ContactColumnNamePhoneContact = "PhoneContact";
	protected static final String c_ContactColumnNameName = "Name";
	
	protected enum ContactType {
		SingleContact (0),
		Group (1);
		
		private int m_Value;
		private ContactType(int value) {
			m_Value = value;
		}
		
		public int getValue() {
			return m_Value;
		}
	}
	
	// Constructors
	
	/**
	 * Protected Constructor for new instances
	 */
	protected Contact() {
		super();
	}
	
	/**
	 * Protected Constructor for instances loaded from the DB.
	 * @param id the existing id of the instance in the DB.
	 */
	protected Contact(int id) {
		super(id);
	}
	
	// Public Methods
	
	/**
	 * @return the CREATE TABLE statement of the Contact table
	 */
	static String GetSqlStatementCreateContactTable() {
		return "CREATE TABLE " + ContactTableName + " (" +
				c_ColumnNameId + 					" INTEGER PRIMARY KEY, " +
				c_ContactColumnNameType + 			" INTEGER NOT NULL, " +
				c_ContactColumnNamePhoneContact + 	" TEXT, " +
				c_ContactColumnNameName + 			" TEXT)";
	}
	
	/**
	 * @return the DROP TABLE statement of the Contact table
	 */
	static String GetSqlStatementDropContactTable() {
		return "DROP TABLE IF EXISTS " + ContactTableName;
	}
	
	/**
	 * @return all contacts in the database
	 */
	static Set<IContact> GetAllContacts(Context context) {
		Set<IContact> contacts = new HashSet<IContact>();
		
		ContactFactory factory = new ContactFactory();
		contacts.addAll(factory.GetAllSingleContacts(context));
		contacts.addAll(factory.GetAllGroups(context));
		
		return contacts;
	}
	
	// DataEntity implementation
	
	/**
	 * @return the name of the table in the DB
	 */
	@Override
	protected Set<String> GetTableNames() {
		Set<String> tableNames = new HashSet<String>();
		tableNames.add(ContactTableName);
		return tableNames;
	}
}
